//
//  CalcView.m
//  JiaoX
//
//  Created by Northwan on 15/1/14.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//  商品数量加减的view

#import "CalcView.h"

#define CALCVIEW_HEIGHT 34
#define CALCVIEW_WIDTH 129
#define BTN_FONT_SIZE 26

@interface CalcView()
@property (strong, nonatomic) UIButton *leftBtn;
@property (strong, nonatomic) UIButton *rightBtn;
@property (assign, nonatomic) NSUInteger prodSum;
@end

@implementation CalcView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _prodSum = 1;
        
        //左边按钮
        _leftBtn = [[UIButton alloc]init];
        UIImage *leftImg = [UIImage resizedImage:@"bg_calc_left"];
        [_leftBtn setFrame:CGRectMake(YSPublicXInset, 0, leftImg.size.width, leftImg.size.height)];
        [_leftBtn setBackgroundImage:leftImg forState:UIControlStateNormal];
        [_leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftBtn];
        
        //中间
        _numLabel = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.leftBtn.frame) + 5,0,40,25)];
        [_numLabel setTitle:@"1" forState:UIControlStateNormal];
        [_numLabel setBackgroundColor:HEXColor(@"#f5f5f5")];
        [_numLabel setCornerRadius:5 borderWidth:1 borderColor:HEXColor(@"#b9b9b9")];
        [_numLabel setUserInteractionEnabled:NO];
        [_numLabel setTitleColor:HEXColor(@"#333333") forState:UIControlStateNormal];
        [_numLabel.titleLabel setFont:SYS_FONT(15)];
        [self addSubview:_numLabel];
        
        //右边
        _rightBtn = [[UIButton alloc]init];
         UIImage *rightImg = [UIImage resizedImage:@"bg_calc_right"];
        [_rightBtn setBackgroundImage:rightImg forState:UIControlStateNormal];
        [_rightBtn setFrame:CGRectMake(CGRectGetMaxX(_numLabel.frame)+5, 0, rightImg.size.width, rightImg.size.height)];
        [_rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightBtn];
        
        //设置自己的宽高
        [self setBackgroundColor:[UIColor clearColor]];
        [self setBounds:CGRectMake(0, 0, CGRectGetMaxX(self.rightBtn.frame),rightImg.size.height)];
        
//        [self.layer setBorderWidth:0.7];
//        [self.layer setCornerRadius:5];
//        [self.layer setMasksToBounds:YES];
    }
    return self;
}

-(void)leftBtnClick:(UIButton *)btn{
    
    --_prodSum;
    [self numChage:_prodSum+1 newNum:_prodSum position:self.tag calcView:self];
    if(_prodSum<1){
        ++_prodSum;
        return;
    }
    [_numLabel setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_prodSum] forState:UIControlStateNormal];
    
}

-(void)rightBtnClick:(UIButton *)btn{
    ++_prodSum;
    [self numChage:_prodSum-1 newNum:_prodSum position:self.tag calcView:self];
    if(_prodSum > 20){
        --_prodSum;
        return;
    }
    [_numLabel setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_prodSum] forState:UIControlStateNormal];
}

-(void)setNum:(NSUInteger)num{
    _prodSum = num;
    [_numLabel setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)num] forState:UIControlStateNormal];
}

-(void)numChage:(NSUInteger)oldNum newNum:(NSUInteger)newNum position:(NSUInteger)position calcView:(CalcView *)calcView{
    if ([_delegate respondsToSelector:@selector(numChange:newNum:calcView:)]) {
        [_delegate numChange:oldNum newNum:newNum calcView:calcView];
    }
}

-(NSUInteger)getNum{
    return [_numLabel.titleLabel.text integerValue];
}

@end
